﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StudentManagement.Utils;
using System.IO;
using System.Globalization;

namespace StudentManagement
{
    public class StudentManage
    {
        private static readonly object _lock = new object();
        private static StudentManage instance;

        private StudentManage()
        {
            ListStudent = null;
            NumberStudent = 0;
        }

        public static StudentManage GetInstance()
        {
            lock (_lock)
            {
                if (instance == null)
                {
                    instance = new StudentManage();
                }
                return instance;
            }
        }

        // ListStudent
        public ListStudent ListStudent { get; set; } // First Student
        public int NumberStudent { get; set; }

        public void InsertNewStudent(Student student)
        {
            ListStudent interior = ListStudent;

            // List is Null
            if (interior == null)
            {
                ListStudent = new ListStudent() { Student = student, NextStudent = null };
            }
            else
            {
                if (Util.CompareString(interior.Student.FullName, student.FullName) == 1)
                {
                    ListStudent = new ListStudent() { Student = student, NextStudent = interior };
                }
                else
                {
                    while (true)
                    {
                        // is Tail
                        if (interior.NextStudent == null)
                        {
                            break;
                        }

                        // Compare student with interior and nextinterior
                        int compareCurrent = Util.CompareString(interior.Student.FullName, student.FullName);
                        int compareNextCurrent = Util.CompareString(interior.NextStudent.Student.FullName, student.FullName);

                        if ((compareCurrent == -1 | compareCurrent == 0) && compareNextCurrent == 1)
                        {
                            break;
                        }
                        interior = interior.NextStudent;   
                    }

                    interior.NextStudent = new ListStudent { Student = student, NextStudent = interior.NextStudent };
                }
            }
            NumberStudent++;
        }

        public List<Student> InteriorListStudent()
        {
            List<Student> listStudent = new List<Student>();
            ListStudent interior = ListStudent;

            while(true)
            {
                if (interior == null)
                {
                    break;
                }

                if (interior.Student != null)
                {
                    listStudent.Add(interior.Student);
                }
                interior = interior.NextStudent;
            }

            return listStudent;
        }

        // Save File
        public void SaveFile(string pathFile)
        {
            StreamWriter writer = new StreamWriter(pathFile);

            writer.WriteLine(NumberStudent);

            ListStudent interior = ListStudent;

            int count = 0;

            while (interior != null)
            {
                if (interior.Student != null)
                {
                    writer.WriteLine(interior.Student.FullName);
                    writer.WriteLine(interior.Student.Birthday.ToString(Constants.DateTimeFormat));
                    writer.WriteLine(interior.Student.Class);
                    writer.WriteLine(interior.Student.Address);
                }
                count++;
                interior = interior.NextStudent;
            }

            writer.Close();
        }

        public void LoadFile(string pathFile)
        {
            if (File.Exists(pathFile))
            {
                var file = File.Open(pathFile, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                StreamReader reader = new StreamReader(file);

                int count;
                if (Int32.TryParse(reader.ReadLine(), out count))
                {
                    ListStudent = null;
                    for (int i = 0; i < count; i++)
                    {
                        string name = reader.ReadLine();
                        DateTime datetime;
                        if (!DateTime.TryParseExact(reader.ReadLine(), "dd/MM/yyyy", null, DateTimeStyles.None, out datetime))
                        {
                            datetime = DateTime.Now;
                        }
                        string Class = reader.ReadLine();
                        string address = reader.ReadLine();

                        Student student = new Student { FullName = name, Birthday = datetime, Class = Class, Address = address };
                        InsertNewStudent(student);
                    }
                }
                file.Close();
            }
        }
    }
}
